package com.stony.orc;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>orcTest
 * <p>com.stony.orc
 *
 * @author stony
 * @version 下午6:34
 * @since 2017/11/15
 */
public class KeyWords {
    static Pattern SEX_AGE_PATTERN = Pattern.compile("(性别:)([男女]+)(年龄:)([1-9]+)(岁)");
    static Pattern PRICE_PATTERN = Pattern.compile("([0-9]+)([\\.]?)([0-9]+?)");
    //    static Pattern DATE_TIME_PATTERN = Pattern.compile("2017年11月08日14时11分");
    static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");

    Map<String, String> keyWords = new HashMap<>();
    List<Tonic> tonicList = new ArrayList<>(64);
    public List<KeyWordProcess> support(String word) {
        List<KeyWordProcess> list = new ArrayList<>();
        for (KeyWordProcess process : KeyWordProcess.values()) {
            if (process.support(word)) {
                list.add(process);
            }
        }
        return list;
    }
    public void process(String word) {
        List<KeyWordProcess> list = support(word);
        if (null != list && !list.isEmpty()) {
            for(KeyWordProcess process : list) {
                String value = keyWords.get(process.getKey());
                if (ImageUtils.isEmpty(value)) {
                    keyWords.put(process.getKey(), process.getWork(word));
                } else {
                    keyWords.put(process.getKey(), (value + process.getWork(word)));
                }
            }
        }
    }
    public List<Tonic> processTonic() {
        if(tonicList != null && tonicList.size() >= 1) {
            return tonicList;
        }
        String word = keyWords.get(KeyWordProcess.TONIC.getKey());
        if(ImageUtils.isNotEmpty(word)) {
            Iterable<String> it = Splitter.on("P")
                    .trimResults()
                    .omitEmptyStrings()
                    .split(word);
            for (String s : it) {
                addTonicToList(s, tonicList);
            }
        }
        return tonicList;
    }
    void addTonicToList(String s, List<Tonic> list) {
        try{
            String[] ss = s.split("/g");
            if(ss.length == 2) {
                list.add(new Tonic(ss[0], Double.valueOf(ss[1].replace("克", ""))));
            }
        }catch (Exception e){
            System.out.println("解析Tonic到List错误:" + e.getMessage());
        }
    }

    public List<Tonic> getTonicList() {
        if(tonicList == null || tonicList.isEmpty()) {
            return processTonic();
        }
        return tonicList;
    }
    public Map<String, String> getKeyWords() {
        return keyWords;
    }
    public enum KeyWordProcess {
        PRICE("price", "价格") {
            @Override
            protected String doWork(String word) {
                Matcher matcher = PRICE_PATTERN.matcher(word);
                if (matcher.find()) {
                    return matcher.group();
                }
                return null;
            }

            @Override
            public boolean support(String word) {
                return word.endsWith("元");
            }
        },
        DATE_TIME("date_time", "日期") {
            @Override
            protected String doWork(String word) {
                try {
                    //milliseconds
                    Date date = DATE_TIME_FORMAT.parse(word);
                    return String.valueOf(date.getTime());
                } catch (ParseException e) {
                    System.out.println("获取日期错误： " + e.getMessage());
                    return null;
                }
            }

            @Override
            public boolean support(String word) {
                return word.contains("年") && word.contains("月");
            }
        },
        USER_NAME("user_name", "姓名") {
            @Override
            protected String doWork(String word) {
                return doSplitterWord(word);
            }

            @Override
            public boolean support(String word) {
                return supportStartsWith(word);
            }
        },
        DOCTOR("doctor", "医师签名") {
            @Override
            protected String doWork(String word) {
                return doSplitterWord(word);
            }

            @Override
            public boolean support(String word) {
                return supportStartsWith(word);
            }
        },
        HOSPITAL_NUMBER("hospital_number", "定点医疗机构编码") {
            @Override
            protected String doWork(String word) {
                return doSplitterWord(word);
            }

            @Override
            public boolean support(String word) {
                return supportStartsWith(word);
            }
        },
        TONIC("tonic", "处方") {
            @Override
            protected String doWork(String word) {
                return word;
            }
            @Override
            public boolean support(String word) {
                return word.startsWith("P") || (word.contains("/g") && word.contains("克"));
            }
        },
        TONIC_NUMBER("tonic_number", "处方号") {
            @Override
            protected String doWork(String word) {
                return doSplitterWord(word);
            }

            @Override
            public boolean support(String word) {
                return supportStartsWith(word);
            }
        },
        AGE("age", "年龄") {
            @Override
            protected String doWork(String word) {
                Matcher matcher = SEX_AGE_PATTERN.matcher(word);
                if (matcher.find()) {
                    if (matcher.groupCount() >= 4) {
                        return matcher.group(4);
                    }
                }
                return null;
            }

            @Override
            public boolean support(String word) {
                return word.contains(msg);
            }
        },
        SEX("sex", "性别") {
            @Override
            protected String doWork(String word) {
                Matcher matcher = SEX_AGE_PATTERN.matcher(word);
                if (matcher.find()) {
                    if (matcher.groupCount() >= 2) {
                        return matcher.group(2);
                    }
                }
                return null;
            }
            @Override
            public boolean support(String word) {
                return word.contains(msg);
            }
        };

        public String getWork(String word) {
            if (ImageUtils.isEmpty(word)) return null;
            if (!support(word)) return null;
            return doWork(word);
        }

        protected String doSplitterWord(String word) {
            Iterable<String> it = Splitter.on(":")
                    .trimResults()
                    .omitEmptyStrings()
                    .split(word);
            List<String> list = Lists.newArrayList(it.iterator());
            if (list.size() >= 2) {
                return list.get(1);
            }
            return null;
        }

        protected abstract String doWork(String word);

        public abstract boolean support(String word);

        protected boolean supportStartsWith(String word) {
            return word.startsWith(msg);
        }

        String key;
        String msg;

        KeyWordProcess(String key, String msg) {
            this.key = key;
            this.msg = msg;
        }

        public String getKey() {
            return key;
        }
    }
}
